package crm.clientManagement.session;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.system.session.AuditTrailSessionBean;
import commonInfrastructure.system.session.SystemSessionBean;
import crm.clientManagement.entity.AddressEntity;
import crm.clientManagement.entity.CompanyEntity;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class CompanySessionBean {

    @PersistenceContext
    private EntityManager em;
    
    @EJB
    SystemSessionBean systemSession = new SystemSessionBean();
    @EJB
    AuditTrailSessionBean auditSession = new AuditTrailSessionBean();

    public void saveNewCompany(CompanyEntity company) throws ParseException{
        em.persist(company);
        em.flush();
        
        //Audit Trail
        EmployeeEntity user = systemSession.getCurrentUser();
        auditSession.addAuditTrail(systemSession.getToday(), company.getId(), user.getUserName(), "crm.clientManagement", "Sales Lead", "Create", "");
    }

    public void saveChanges(CompanyEntity company, String action) throws ParseException{
        em.merge(company);
        //Audit Trail
        EmployeeEntity user = systemSession.getCurrentUser();
        auditSession.addAuditTrail(systemSession.getToday(), company.getId(), user.getUserName(), "crm.clientManagement", "Client", action, "");
    }
    
    
    public void saveCustomer(CompanyEntity company, List<AddressEntity> billingList, List<AddressEntity> shippingList, String action) throws ParseException{
        for (int i=0;i<billingList.size();i++){
            em.persist(billingList.get(i));
        }
        for (int j=0;j<shippingList.size();j++){
            em.persist(shippingList.get(j));
        }
        company.setBillingAddresses(billingList);
        company.setShippingAddresses(shippingList);
        
        em.merge(company);
        
        //Audit Trail
        EmployeeEntity user = systemSession.getCurrentUser();
        auditSession.addAuditTrail(systemSession.getToday(), company.getId(), user.getUserName(), "crm.clientManagement", "Customer", action, "");
    }

//    public void deleteCompany(Long id) {
//        CompanyEntity company = em.find(CompanyEntity.class, id);
//        em.remove(company);
//    }
    
    public void deleteClient(Long id) throws ParseException{
        CompanyEntity client = em.find(CompanyEntity.class,id);
        List <AddressEntity> billingAddresses = client.getBillingAddresses();
        for (Object o: billingAddresses){
            AddressEntity ads = (AddressEntity)o;
            em.remove(ads);
        }
        List <AddressEntity> shippingAddresses = client.getShippingAddresses();
        for (Object o: shippingAddresses){
            AddressEntity ads = (AddressEntity)o;
            em.remove(ads);
        }
        em.remove(client);
        
        //Audit Trail
        EmployeeEntity user = systemSession.getCurrentUser();
        auditSession.addAuditTrail(systemSession.getToday(), client.getId(), user.getUserName(), "crm.clientManagement", "Client", "Delete", "");
    }
    
    public List<SelectItem> getShippingAddressList(CompanyEntity company){
        
        if (company == null) {
            return new ArrayList<SelectItem>();
        } else {

            List<AddressEntity> addresses = company.getShippingAddresses();

            List<SelectItem> selectItems = new ArrayList<SelectItem>();


            for (AddressEntity address : addresses) {

                selectItems.add(new SelectItem(address.getId(), address.getStreetName() + ", " + address.getCity() + ", " + address.getPostCode()));
            }
            return selectItems;
        }
    }
    
    public List<SelectItem> getBillingAddressList(CompanyEntity company){
        
        if (company == null) {
            return new ArrayList<SelectItem>();
        } else {

            List<AddressEntity> addresses = company.getBillingAddresses();

            List<SelectItem> selectItems = new ArrayList<SelectItem>();


            for (AddressEntity address : addresses) {

                selectItems.add(new SelectItem(address.getId(), address.getStreetName() + ", " + address.getCity() + ", " + address.getPostCode()));
            }
            return selectItems;
        }
    }

    public List<String> getCompanyNames(){
        Query query=em.createQuery("SELECT c.name FROM CompanyEntity c");
        return query.getResultList();
    }
    
    public List<CompanyEntity> getAllCompanies() {
        Query query = em.createQuery("SELECT sl FROM CompanyEntity sl");
        return query.getResultList();
    }
    

    public CompanyEntity getCompanyById(Long id) {
        return em.find(CompanyEntity.class, id);
    }
    
    public List<CompanyEntity> getCompaniesBySalesman(Long id){
        Query query = em.createQuery("SELECT c FROM CompanyEntity c WHERE c.salesman.id = :salesmanId ");
        query.setParameter("salesmanId", id);
        return query.getResultList();
        
    }
}
